package com.cs252.flashcardz;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {
	private static String url = "jdbc:mysql://mydb.ics.purdue.edu:3306/",
			dbName = "king104", driver = "com.mysql.jdbc.Driver",
			userName = "king104", password = "money99";

	private String currUser = "";

	private static Connection conn;

	public Database() {
		try {
			Class.forName(driver).newInstance();

		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public void connect() throws SQLException {
		conn = DriverManager.getConnection(url + dbName, userName, password);
		System.out.println("Connected database successfully...");
	}

	public void close() throws SQLException {
		System.out.println("Closing Connection...");
		conn.close();
	}

	public Statement getStatement() throws SQLException {
		return conn.createStatement();
	}

	public int register(String user, String pass) throws SQLException {
		// Register uses Sending and Recieving as a reference to use laters.
		ResultSet res = getStatement().executeQuery(
				"SELECT Username FROM  king104.Flash");
		String users = "";
		while (res.next()) {
			users += res.getString("Username");
			users += " ";
		}
		String[] iUser = users.split(" ");
		for (int i = 0; i < iUser.length; i++) {
			if (iUser[i].equals(user)) {
				System.out.println("Username Already In Use");
				return -1;
			}
		}
		int val = getStatement().executeUpdate(
				"INSERT INTO `king104`.`Flash` (`Username`, `Password`, `Script`) VALUES ('"
						+ user + "', '" + pass + "', '0')");
		if (val == 1)
			System.out.println("Successfully inserted value");

		return 0;
	}

	public int login(String user, String pass) throws SQLException {
		ResultSet res = getStatement().executeQuery(
				"SELECT Username, Password FROM  king104.Flash");
		String users = "", passs = "";
		while (res.next()) {
			users += res.getString("Username");
			users += " ";
			passs += res.getString("Password");
			passs += " ";
		}
		String[] iUser = users.split(" "), iPass = passs.split(" ");
		for (int i = 0; i < iUser.length; i++) {
			if (iUser[i].equals(user) && iPass[i].equals(pass)) {
				System.out.println("Welcome " + user + "!");
				currUser = user;
				return 1;
			}
		}
		System.out
				.println("Username and/or Password are incorrect. Please Try again.");
		return -1;
	}

	public void logout() {
		currUser = "";
	}

	public String getScript() throws SQLException {
		ResultSet res = getStatement().executeQuery(
				"SELECT Username, Script FROM  king104.Flash");
		String users = "", script = "";
		while (res.next()) {
			users += res.getString("Username");
			users += " ";
			script += res.getString("Script");
			script += " ";
		}
		String[] iUser = users.split(" "), iscript = script.split(" ");
		for (int i = 0; i < iUser.length; i++) {
			if (iUser[i].equals(currUser)) {
				System.out.println(iscript[i]);
				break;
			}
		}

		return script;
	}

	public void setScript(String newScript) throws SQLException {
		int res = getStatement().executeUpdate(
				"UPDATE `king104`.`Flash` SET `Script`='" + newScript
						+ "' WHERE `Username`='" + currUser + "'");
		if (res == 1)
			System.out.println("Success Update");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Database dt = new Database();
		try {
			dt.connect();
			// dt.register("dgaine3", "ilovechris");
			dt.login("king104", "ilovedestiny");
			Thread.sleep(1000);
			System.out.println(dt.currUser + " is logged in.");
			dt.setScript("Im+a+coconut!");
			dt.getScript();
			Thread.sleep(1000);
			System.out.println(dt.currUser + " is logging out.");
			Thread.sleep(1000);
			dt.logout();
			dt.login("dgaine3", "ilovechris");
			System.out.println(dt.currUser + " is logged in.");
			Thread.sleep(2000);
			System.out.println(dt.currUser + " is logging out.");
			dt.logout();
			Thread.sleep(4000);
			dt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}
